package com.lucweb.interceptor;

import com.lucweb.entity.User;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
 * interceptor
 * @author Zhouzhou
 * @date 2016-1-25
 * @time 下午09:48:21
 */
/**
 * that class want to judge the session is null，it extends the class named : MethodFilterInterceptor,
 */
public class PrivilegeInterceptor extends MethodFilterInterceptor{

    /**
     * the method doIntercept is the main method of the MethodFilterInterceptor
     */
    @Override
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        //judge login
        //
        User adminUser = (User) ServletActionContext.getRequest()
                .getSession().getAttribute("user");
        if(adminUser != null){
            // is logined ,have the userinfo in the session
            return actionInvocation.invoke();
        }else{
            // is not login
            ActionSupport support = (ActionSupport) actionInvocation.getAction();
            support.addActionError("You are not logged in!No permission to access!");
            return "toLogin";
        }
    }
}